package com.library.controller;

import com.library.bean.User;
import com.library.service.UserService;
import com.library.service.impl.UserServiceImpl;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

@WebServlet("/register")
public class RegisterServlet extends HttpServlet {

    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        request.setCharacterEncoding("UTF-8");
        UserService us = (UserService) new UserServiceImpl();
        String username = request.getParameter("username");
        String password = request.getParameter("password");
        String realname = request.getParameter("realName");
        User user=new User(username, password, realname);
        int row=us.userRegister(user);
        if (row > 0) {
            request.setAttribute("msg", "注册成功！请返回并登录");
            System.out.println("----");
            request.getRequestDispatcher("login.jsp").forward(request, response);
        }else{
            request.setAttribute("msg", "该用户名已存在，请重新输入");
            request.getRequestDispatcher("register.jsp").forward(request, response);
        }
    }

    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        // 跳转到登录页面

    }
}
